package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2020-08-13 10:13
 * @description
 */
@Controller
@RequestMapping("/ret")
public class demo2_RetController {
    /**
     *
     * 返回 ModelAndView 视图对象 如果有数据
     * 不太推荐 太麻烦了
     */
    @RequestMapping("/foo1")
    public ModelAndView foo1(ModelAndView modelAndView){


        modelAndView.addObject("info","啊实打实");
        modelAndView.setViewName("ret/success");
        return modelAndView;
    }

    /**
     * 返回字符串视图 需要数据 使用model就行了!!!!
     */
    @RequestMapping("/foo2")
    public String foo2(Model model){

        //如果需要数据 放到jsp页面
        model.addAttribute("info","阿萨德哈吉斯");

        return "ret/success";
    }

    /**
     *返回字符串视图 加上前缀 forward 表示请求转发 不写默认值 傻子才写呢
     *
     * forward 不能够使用 前缀和后缀的
     */
    @RequestMapping("/foo3")
    public String foo3(Model model){
        //如果需要数据 放到jsp页面
        model.addAttribute("info","阿萨德哈吉斯");
        return "forward:/WEB-INF/pages/ret/success.jsp";
    }

    /**
     * 重定向  卵子用没有!!!
     * @return
     */
    @RequestMapping("/foo4")
    public String foo4(){
        return "redirect:http://www.baidu.com";
    }


    /**
     * 需求 我就想给浏览器返回一个 success的字符串
     */
    @RequestMapping("/foo5")
    public void foo5(HttpServletResponse response) throws IOException {

        response.getWriter().print("success");
    }

    /**
     *
     * 返回一个user对象 转换json格式的字符串!!!
     *
     */
    @RequestMapping("/foo6")
    public void foo6(HttpServletResponse response) throws IOException {
        User user = new User(1, "小明", "123", "123@qq.com");

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(user);

        //中文乱码
        response.setContentType("application/json;charset=utf-8");

        response.getWriter().print(s);
    }


    /**
     * 需求 我就想给浏览器返回一个 success的字符串
     */
    @RequestMapping("/foo51")
    @ResponseBody
    public String foo51() throws IOException {

        return "success";
    }

    /**
     *
     * 返回一个user对象 转换json格式的字符串!!!
     *
     */
    @RequestMapping("/foo61")
    @ResponseBody
    public User foo61() throws IOException {
        User user = new User(1, "小明", "123", "123@qq.com");

        return user;
    }

    /**
     * RequestBody 我要去从请求体 拿json字符串转对象!!!
     *
     */
    @RequestMapping("/foo7")
    @ResponseBody
    public String foo7(@RequestBody User user) throws IOException {
        System.out.println(user);


        return "success";
    }








}
